package com.mall.service.impl;

import com.mall.constant.StatusConstant;
import com.mall.mapper.BrandMapper;
import com.mall.mapper.GoodsMapper;
import com.mall.mapper.OrdersMapper;
import com.mall.mapper.UserMapper;
import com.mall.po.Orders;
import com.mall.service.WorkSpaceService;
import com.mall.vo.BusinessDataVO;
import com.mall.vo.OverviewBrandsVO;
import com.mall.vo.OverviewGoodsVO;
import com.mall.vo.OverviewOrdersVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class WorkSpaceServiceImpl implements WorkSpaceService {

    private final OrdersMapper ordersMapper;

    private final UserMapper userMapper;

    private final BrandMapper brandMapper;

    private final GoodsMapper goodsMapper;

    /**
     * 查询今日运营数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {

        /**
         * 1.查询总订单数
         * 2.查询有效订单数
         * 3.查询订单完成率 = 有效订单数 / 总订单数
         * 4.平均客单价 = 营业额 / 有效订单数
         * 5.营业额
         * 6.新增用户数
         */

        Map<String, Object> params = new HashMap<>();
        params.put("beginTime", begin);
        params.put("endTime", end);
        // 查询总订单数
        Integer totalOrderCount = ordersMapper.countOrders(params);
        // 查询有效订单数
        params.put("status", Orders.COMPLETED);
        Integer validOrderCount = ordersMapper.countOrders(params);
        // 查询营业额
        Double turnover = ordersMapper.getTurnover(params);
        turnover = turnover == null ? 0.0 : turnover;
        // 订单完成率
        double orderCompletionRate = 0.0;
        if (totalOrderCount != 0) {
            orderCompletionRate = validOrderCount / Double.valueOf(totalOrderCount);
        }
        // 平均客单价
        double unitPrice = 0.0;
        if (validOrderCount != 0) {
            unitPrice = turnover / validOrderCount;
        }
        // 新增用户数
        Integer newUsers = userMapper.countUser(params);
        return BusinessDataVO.builder()
                            .validOrderCount(validOrderCount)
                            .turnover(turnover)
                            .orderCompletionRate(orderCompletionRate)
                            .unitPrice(unitPrice)
                            .newUsers(newUsers)
                            .build();
    }

    /**
     * 查询今日订单管理数据
     * @return
     */
    @Override
    public OverviewOrdersVO getOverviewOrders() {
        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
        LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);
        Map<String, Object> params = new HashMap<>();
        params.put("beginTime", begin);
        params.put("endTime", end);
        // 查询今日总订单数
        Integer totalOrderCount = ordersMapper.countOrders(params);
        // 查询今日待接单数
        params.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = ordersMapper.countOrders(params);
        // 查询今日待派送数
        params.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = ordersMapper.countOrders(params);
        // 查询今日已完成数
        params.put("status", Orders.COMPLETED);
        Integer completedOrders = ordersMapper.countOrders(params);
        // 查询今日已取消数
        params.put("status", Orders.CANCELLED);
        Integer cancelledOrders = ordersMapper.countOrders(params);
        return OverviewOrdersVO.builder()
                                .allOrders(totalOrderCount)
                                .waitingOrders(waitingOrders)
                                .deliveredOrders(deliveredOrders)
                                .completedOrders(completedOrders)
                                .cancelledOrders(cancelledOrders)
                                .build();
    }

    /**
     * 查询今日品牌数据
     * @return
     */
    @Override
    public OverviewBrandsVO getOverviewBrands() {

        // 查询已上架品牌数
        Integer sold = brandMapper.countBrand(StatusConstant.ENABLE);
        // 查询已停用品牌数
        Integer discontinued = brandMapper.countBrand(StatusConstant.DISABLE);
        return OverviewBrandsVO.builder()
                                .sold(sold)
                                .discontinued(discontinued)
                                .build();
    }

    /**
     * 查询今日商品数据
     * @return
     */
    @Override
    public OverviewGoodsVO getOverviewGoods() {
        // 查询已上架商品数
        Integer sold = goodsMapper.countBrand(StatusConstant.ENABLE);
        // 查询已停用商品数
        Integer discontinued = goodsMapper.countBrand(StatusConstant.DISABLE);
        return OverviewGoodsVO.builder()
                                .sold(sold)
                                .discontinued(discontinued)
                                .build();
    }
}
